package model;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class Order {
    private int orderID;
    private String customerName;
    private int storeID;
    private Timestamp orderTime;
    private double totalPrice;
    private List<OrderDetail> orderDetails;

    public Order(int orderID, String customerName, int storeID, Timestamp orderTime, double totalPrice) {
        this.orderID = orderID;
        this.customerName = customerName;
        this.storeID = storeID;
        this.orderTime = orderTime;
        this.totalPrice = totalPrice;
        this.orderDetails = new ArrayList<>();
    }

    // Getters and Setters
    public int getOrderID() {
        return orderID;
    }

    public void setOrderID(int orderID) {
        this.orderID = orderID;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public int getStoreID() {
        return storeID;
    }

    public void setStoreID(int storeID) {
        this.storeID = storeID;
    }

    public Timestamp getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(Timestamp orderTime) {
        this.orderTime = orderTime;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public List<OrderDetail> getOrderDetails() {
        return orderDetails;
    }

    public void addOrderDetail(OrderDetail detail) {
        this.orderDetails.add(detail);
        updateTotalPrice(); // 每次添加订单详情时更新总金额
    }

    public void updateTotalPrice() {
        this.totalPrice = getTotalCost(); // 将动态计算的总金额设置为 totalPrice 字段
    }

    public double getTotalCost() {
        return orderDetails.stream()
                .mapToDouble(detail -> detail.getItem().getPrice() * detail.getQuantity())
                .sum();
    }




}
